package com.syl.servlet;

import com.syl.entity.User;
import com.syl.service.UserService;
import com.syl.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;

/**
 * @Classname UserRegisterServlet
 * @Description
 * @Date 2025/8/2 10:23
 * @Created by SYL
 */

@WebServlet("/register")

public class UserRegisterServlet extends HttpServlet {
    UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        int sexValue = "1".equals(sex) ? 1 : 0;



        System.out.println("username = " + username);
        System.out.println("password = " + password);
        System.out.println("phone = " + phone);
        System.out.println("sex = " + sex);

        //req.getSession().setAttribute("username",username);
        User user = new User(username, password, phone, new Date(System.currentTimeMillis()), sexValue);
        boolean register = userService.register(user);

        resp.setContentType("text/html;charset=UTF-8");  //设置响应体编码  设置MIME类型和字符集
        PrintWriter writer = resp.getWriter();
        if (register) {
            System.out.println("注册成功");
            writer.println("<h1>嗨嗨 成功了老铁!</h1>");
            writer.println(" <a href=\"logic.html\">立即登录</a>");

        } else {
            System.out.println("注册失败");
            writer.println("<h1>嗨嗨 失败了老铁!</h1>");
        }





    }
}
